﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class CityForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CountryEntity countryE = new CountryEntity();
            CountryService countryS = CountryService.GetInstance();
            IList<CountryEntity> lstCountry = countryS.LoadAll();
            if (null != lstCountry)
            {
                this.storeCountry.DataSource = lstCountry;
                this.storeCountry.DataBind();
            }
        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string cityId)
        {
            CityEntity cityE = new CityEntity();
            CityService cityS = CityService.GetInstance();
            cityE.CityId = int.Parse(cityId);
            cityE = cityS.LoadByPrimaryKey(cityE);
            if (null == cityE)
                return;
            hdfCityId.Value = cityE.CityId;
            txtCityName.Text = cityE.CityName;
            cbbCountry.SetValue(cityE.CountryId);
            txtDescription.Text = cityE.Description;
            ckbIsActive.Checked = cityE.IsAcvite;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            CityEntity cityE = new CityEntity();
            CityService cityS = CityService.GetInstance();
            if ("" != hdfCityId.Value.ToString())
            {
                string cityId = hdfCityId.Value.ToString();
                cityE.CityId = int.Parse(cityId);
                cityE = cityS.LoadByPrimaryKey(cityE);
                cityE.CityName = txtCityName.Text;
                cityE.CountryId = int.Parse(cbbCountry.SelectedItem.Value);
                cityE.Description = txtDescription.Text;
                cityE.IsAcvite = ckbIsActive.Checked;
                cityE.UpdateDate = DateTime.Now;
                cityE.UpdateBy = FSSContext.Current.User.EmployeeId;
                cityS.Update(cityE);
            }
            else
            {
                cityE.CityName = txtCityName.Text;
                cityE.CountryId = int.Parse(cbbCountry.SelectedItem.Value);
                cityE.Description = txtDescription.Text;
                cityE.IsAcvite = ckbIsActive.Checked;
                cityE.CreateDate = DateTime.Now;
                cityE.CreateBy = FSSContext.Current.User.EmployeeId;
                cityE.UpdateDate = DateTime.Now;
                cityE.UpdateBy = FSSContext.Current.User.EmployeeId;
                cityS.Insert(cityE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}